var MongoClient = require("mongodb").MongoClient
var DB_CONN_STR = "mongodb://test:123456@localhost:27017/test"

var insertData = function (db, callback) {
  //连接到表 site
  var collection = db.collection("site")
  //插入数据
  var data = [
    { name: "菜鸟教程", url: "www.runoob.com" },
    { name: "菜鸟工具", url: "c.runoob.com" },
  ]
  collection.insert(data, function (err, result) {
    if (err) {
      console.log("Error:" + err)
      return
    }
    callback(result)
  })
}

MongoClient.connect(DB_CONN_STR, function (err, db) {
  if (err) {
    return console.log("Error: " + err)
  }

  console.log("连接成功！")
  insertData(db, function (result) {
    console.log(result)
    db.close()
  })
})
